package com.zh.media.controller;

import com.zh.common.annotation.NeedAuth;
import com.zh.common.annotation.NeedPermission;
import com.zh.common.annotation.NeedRoles;
import com.zh.common.annotation.Operator;
import com.zh.common.constant.AuthConstant;
import com.zh.media.domain.dto.*;
import com.zh.web.domain.media.vo.ArticleVo;
import com.zh.media.service.ArticleService;
import com.zh.web.base.ApiResult;
import com.zh.web.base.BaseController;
import com.zh.web.base.PageApiResult;
import com.zh.web.domain.media.entity.Article;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "文章管理接口")
@Slf4j
@RequestMapping("/article")
@RequiredArgsConstructor
@RestController
@Validated
@Operator
public class ArticleController extends BaseController {

    private final ArticleService articleService;

    @ApiOperation(value = "用户上传文章")
    @PostMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.USER})}, permissions = {@NeedPermission(value = {AuthConstant.INSERT})})
    public ApiResult<String> addArticle(@Valid @RequestBody AddArticleDto addArticleDto) {
        addArticleDto.setCreateOperator(operator);
        addArticleDto.setUpdateOperator(operator);
        return buildMsgPackage(articleService.addArticle(addArticleDto));
    }

    @ApiOperation(value = "用户修改文章")
    @PutMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.USER})}, permissions = {@NeedPermission(value = {AuthConstant.UPDATE})})
    public ApiResult<String> updateArticle(@Valid UpdateArticleDto updateArticleDto) {
        updateArticleDto.setCreateOperator(operator);
        updateArticleDto.setUpdateOperator(operator);
        return buildMsgPackage(articleService.updateArticle(updateArticleDto));
    }

    @ApiOperation(value = "用户删除文章")
    @DeleteMapping("/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.USER})}, permissions = {@NeedPermission(value = {AuthConstant.DELETE})})
    public ApiResult<String> deleteArticle(@PathVariable("id") Long id) {
        return buildMsgPackage(articleService.deleteArticle(id));
    }

    @ApiOperation(value = "id 查询文章详情")
    @GetMapping
    public ApiResult<ArticleVo> getArticle(@RequestParam("id") Long id, HttpServletRequest request) {
        return buildSuccessPackage(articleService.getDetail(id,request));
    }
    @PostMapping("/page")
    @ApiOperation("分页查文章 标题 内容模糊")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.ADMIN})}, permissions = {@NeedPermission(value = {AuthConstant.SELECT})})
    public ApiResult<PageApiResult<ArticleVo>> pageArticle(@RequestBody PageArticleDto pageArticleDto) {
        return buildSuccessPackage(articleService.pageArticle(pageArticleDto));
    }
    @PostMapping("/list")
    @ApiOperation(value = "ids查询",hidden = true)
    public ApiResult<List<ArticleVo>> listArticle(@RequestBody List<Long> ids) {
        return buildSuccessPackage(articleService.listArticleVoByIds(ids));
    }
    @GetMapping("/list/after")
    @ApiOperation(value = "最近Time分钟文章",hidden = true)
    public ApiResult<List<ArticleVo>> listArticleAfterTime(@RequestParam("time")LocalDateTime time){
        return buildSuccessPackage(articleService.listArticleAfterTime(time));
    }
    @PutMapping("/status")
    @ApiOperation(value = "管理修改文章状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.ADMIN})}, permissions = {@NeedPermission(value = {AuthConstant.UPDATE})})
    public ApiResult<String> auditArticle(@RequestBody AuditArticleDto auditArticleDto) {
        auditArticleDto.setUpdateOperator(operator);
        return buildMsgPackage(articleService.statusArticle(auditArticleDto));
    }


    @PostMapping("/addBatch")
    @ApiOperation(value = "批量插入", hidden = true)
    public ApiResult<String> addArticleBatch(@RequestBody List<Article> list) {
        return buildMsgPackage(articleService.saveList(list));
    }

    @PostMapping("/praise")
    @ApiOperation(value = "点赞/取消点赞文章",notes = "只传入praise")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.USER)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> praiseArticle(@Valid @RequestBody UpdateWorkInfoDto updateWorkInfoDto) {
        return buildMsgPackage(articleService.praiseArticle(updateWorkInfoDto));
    }

    @PostMapping("/collect")
    @ApiOperation(value = "收藏/取消收藏文章",notes = "只传入collect")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.USER)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> collectArticle(@Valid @RequestBody UpdateWorkInfoDto updateWorkInfoDto) {
        return buildMsgPackage(articleService.collectArticle(updateWorkInfoDto));
    }
    @PostMapping("/view")
    @ApiOperation(value = "更新浏览量,不传默认为1",notes = "只传入view")
    public ApiResult<Integer> updateView(@Valid @RequestBody UpdateWorkInfoDto updateWorkInfoDto){
        return buildSuccessPackage(articleService.updateView(updateWorkInfoDto));
    }
    @PostMapping("/share")
    @ApiOperation(value = "更新分享量,不传默认为1",notes = "只传入share")
    public ApiResult<Integer> updateShare(@Valid @RequestBody UpdateWorkInfoDto updateWorkInfoDto){
        return buildSuccessPackage(articleService.updateShare(updateWorkInfoDto));
    }

    @GetMapping("/praise/count")
    @ApiOperation(value = "获取文章点赞数")
    public ApiResult<Integer> getArticlePraise(@RequestParam("id")Long id){
        return buildSuccessPackage(articleService.getArticlePraise(id));
    }

}
